package com.example.musicapp.storage.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

/**
 * @author tangan
 */
public class MyDatabaseHelper extends SQLiteOpenHelper {
    /**
     * 数据库名字
     */
    public static final String DB_NAME = "music_list.db";

    /**
     * 数据库版本号
     */
    public static final int DB_VERSION = 1;



    /**
     * volatile 修饰它
     */
    private static volatile MyDatabaseHelper sMyDatabaseHelper;

    /**
     * 表
     * @author tangan
     */
    public interface TABLE {
        String HISTORY_NOTE = "history_search";
    }


    /**
     * 双重锁判定单例
     * @param context 上下文
     * @return 返回单例对象
     */
    public static MyDatabaseHelper getInstance(Context context) {
        if (sMyDatabaseHelper == null) {
            synchronized (MyDatabaseHelper.class) {
                if (sMyDatabaseHelper == null) {
                    sMyDatabaseHelper = new MyDatabaseHelper(context);
                }
            }
        }
        return sMyDatabaseHelper;
    }


    /**
     * 创建历史记录的表
     */
    public static final String CREATE_HISTORY_SEARCH_TABLE = "create table history_search(" +
            "id integer primary key autoincrement," +
            "record text)";

    public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_HISTORY_SEARCH_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    /**
     *     创建一个新的构造方法
     * @param context 上下文
     */
    public MyDatabaseHelper(Context context){
        super(context,DB_NAME,null,DB_VERSION);
    }
}
